package mrms.api.repository;

import mrms.api.entity.MeetingEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

/**
 * @author lenchu
 * @date 2019/02/06
 */
public interface MeetingRepository extends JpaRepository<MeetingEntity, Integer> {

    /**
     * 通过邀请码查看会议
     * @param invitationcode 邀请码
     * @return
     */
    Optional<MeetingEntity> findByInvitationcode(String invitationcode);

    /**
     * 通过会议状态查看 并按开始时间降序排列
     * @param state
     * @param pageable
     * @return
     */
    Page<MeetingEntity> findAllByStateOrderByStarttimeDesc(Integer state, Pageable pageable);

    /**
     * 查询所有会议 并按开始时间降序排列
     * @param pageable
     * @return
     */
    Page<MeetingEntity> findAllByIdNotNullOrderByStarttimeDesc(Pageable pageable);

    /**
     * 通过发起人查看
     * @param sponsor
     * @param pageable
     * @return
     */
    Page<MeetingEntity> findAllBySponsor(String sponsor, Pageable pageable);

    /**
     * 通过id列表查询所有
     * @param ids
     * @param pageable
     * @return
     */
    Page<MeetingEntity> findAllByIdIsIn(Iterable<Integer> ids, Pageable pageable);

    /**
     * 通过id列表查询所有或者通过发起人查看
     * @param ids
     * @param sponsor
     * @param pageable
     * @return
     */
    Page<MeetingEntity> findAllByIdIsInOrSponsor(Iterable<Integer> ids, String sponsor, Pageable pageable);

    /**
     * 通过发起人和会议id查询
     * @param sponsor
     * @param id
     * @return
     */
    Optional<MeetingEntity> findBySponsorAndId(String sponsor, Integer id);

    /**
     * 通过room查询
     * @param room
     * @return
     */
    Page<MeetingEntity> findAllByRoom(String room, Pageable pageable);

    List<MeetingEntity> findAllByRoomAndStarttimeBetween(String room, Long start, Long end);



    List<MeetingEntity> findByStarttimeBetweenAndAdjustOrderByStarttime(Long oneminbefore,Long oneminafter,int adjust);

    List<MeetingEntity> findByStarttimeBetweenAndEndtimeBetweenAndRoom(Long starttime,Long endtime,Long nst,Long ned,String room);

    List<MeetingEntity> findByRoomOrderByEndtimeDesc(String room);

    List<MeetingEntity> findByStarttimeLessThanEqualAndEndtimeGreaterThanEqualAndRoom(long starttime,long endtime,String room);

    MeetingEntity findById(int id);
}
